﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Reflection;
using System.Collections;
using System.ComponentModel;

namespace MiData
{
    /// <summary>
    /// 查询语句参数
    /// </summary>
    public class MiParameter
    {
        /// <summary>
        /// 参数名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 值
        /// </summary>
        public object Vane { get; set; }

        public MiParameter(string n, object v)
        {
            this.Name = n;
            this.Vane = v;
        }
    }

    public class PropertyChangedBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }


    /// <summary>
    /// 模拟表映射 所有表需要主键
    /// </summary>
    public  abstract partial class MiActiveRecord : PropertyChangedBase
    {
        public static BaseDBAdapter DB;

        public DbConnection Conn
        {
            get
            {
                return DB.Connection;
            }
        }

        #region 数据操作规类方法
        
        internal DbCommand CreateCommand()
        {
            return DB.CreateCommand();
        }

        internal DbCommand CreateCommand(string sql)
        {
            return DB.CreateCommand(sql);
        }

        internal DbCommand CreateCommand(string sql, params MiParameter[] ps)
        {
            DbCommand cmd = this.CreateCommand(sql);
            BuildParameter(cmd, ps);
            return cmd;
        }

        internal DbCommand CreateCommand(string sql, MiParameter p)
        {
            DbCommand cmd = this.CreateCommand(sql);
            BuildParameter(cmd, p);
            return cmd;
        }

        internal DbDataReader ExecuteQuery(string sql)
        {
            return DB.QuickExecuteQuery(sql);
        }

        internal int ExecuteSqlNoQuery(string sql)
        {
            return DB.QuickExecuteNoQuery(sql);
        }

        internal long GetLastInsertID()
        {
            return DB.GetLastInsertID();
        }


        /// <summary>
        /// 给DBCommand添加多个参数
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="ps"></param>
        internal void BuildParameter(DbCommand cmd, params MiParameter[] ps)
        {
            foreach (MiParameter pa in ps)
            {
                DbParameter p = cmd.CreateParameter();
                p.ParameterName = pa.Name;
                p.Value = pa.Vane;
                cmd.Parameters.Add(p);
            }
        }

        /// <summary>
        /// 将行记录映射至对象，必须确保实体的字段名称和数据库字段名称保持一致
        /// </summary>
        /// <param name="sp"></param>
        /// <param name="reader"></param>
        /// <returns></returns>
        internal object BuildEntity(object sp, DbDataReader reader)
        {
            PropertyInfo[] ps = sp.GetType().GetProperties();
            foreach (PropertyInfo proInfo in ps)
            {
                string name = proInfo.Name;
                object obj = null;

                try
                {
                    obj = reader[name];
                }
                catch (Exception)
                {
                    continue;
                }

                if (obj == null || obj is System.DBNull) continue;


                string dbValue = obj.ToString();
                object entiryValue = null;

                if (proInfo.PropertyType == typeof(string))
                {
                    entiryValue = dbValue;
                }
                else if (proInfo.PropertyType == typeof(int))
                {
                    entiryValue = int.Parse(dbValue.ToString());
                }
                else if (proInfo.PropertyType == typeof(long))
                {
                    entiryValue = long.Parse(dbValue.ToString());
                }
                else if (proInfo.PropertyType == typeof(TimeSpan))
                {
                    entiryValue = TimeSpan.FromTicks(long.Parse(dbValue));
                }
                else if (proInfo.PropertyType == typeof(DateTime))
                {
                    entiryValue = System.DateTime.FromBinary(long.Parse(dbValue));
                }
                else if (proInfo.PropertyType.BaseType == typeof(Enum))
                {
                    entiryValue = Enum.Parse(proInfo.PropertyType, dbValue);
                }

                if (entiryValue != null)
                {
                    proInfo.SetValue(sp, entiryValue, null);
                }
            }
            return sp;
        }

        #endregion
    }
}
